﻿using Hx.ADSyncPlatform.Db;
using Hx.ADSyncPlatform.Entity.Entities;
using Hx.ADSyncPlatform.Infrastructure.Expression.Common;
using Hx.ADSyncPlatform.Infrastructure.Page;
using Hx.ADSyncPlatform.Service.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Hx.ADSyncPlatform.Service
{
    public class Plat_OperateLogService : BaseService<Plat_OperateLog>
    {
        public Plat_OperateLogService(DbContext db) : base(db)
        {
        }

        /// <summary>
        /// 系统操作日志列表 
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public async Task<PageResult<Plat_OperateLog>> GetPageList(Dictionary<string, string> param, Pagination pagination)
        {

            RefAsync<int> totalCount = 0;

            Expression<Func<Plat_OperateLog, bool>> expression = n => true;

            if (param.ContainsKey("beginTime"))
            {
                expression = expression.And(n => n.OperateTime >= Convert.ToDateTime(param["beginTime"]));
            }
            if (param.ContainsKey("endTime"))
            {
                expression = expression.And(n => n.OperateTime <= Convert.ToDateTime(param["endTime"]));
            }
            if (param.ContainsKey("operatePlatManager"))
            {
                expression = expression.And(n => n.OperatePlatManager.Contains(param["operatePlatManager"]));
            }
            if (param.ContainsKey("behaviorType"))
            {
                expression = expression.And(n => n.BehaviorType.Equals(param["behaviorType"]));
            }

            if (param.ContainsKey("operateResult"))
            {
                expression = expression.And(n => n.OperateResult.Equals(param["operateResult"]));
            }

            var res = await base.dbClient.Queryable<Plat_OperateLog>()
                .Where(expression)
                 .OrderBy(n => n.OperateTime, OrderByType.Desc)
                 .ToPageListAsync(pagination.pageNum, pagination.pageSize, totalCount);


            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pagination.pageSize.ObjToDecimal())).ObjToInt();
            pagination.pageCount = pageCount;
            pagination.dataCount = totalCount.Value;

            return new PageResult<Plat_OperateLog>(data: res, pagination: pagination);
        }
    }
}
